package com.project.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.project.common.ConfigBean;



/**
 * 字符串加密
 * @author
 *
 */
@Component
public class EncryptString {

	@Autowired
	public ConfigBean configBean;
	
	/**
	 * MD5加密
	 * @param str
	 * @return 返回32位字符串
	 */
	public String MD5Encrypt(String str){
		String sourceStr = str+configBean.getMd5Salt();
		return encoder(sourceStr);
	}
	
	public String MD5(String str){
		return encoder(str);
	} 
	
    private static String encoder(String sourceStr) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e);
        }
        return result;
    }
    
    
    
    public static void main(String[] args) throws Exception {
    	//System.out.println(encoder("admin"+"xVPWZ8D0sdb81JVB"));
    	
    	
    }
}
